package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 套餐
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Value("${out_put_path}")
    private String outPutPath;

    /**
     * 分页查询  可加条件
     *
     * @param queryPageBean 实体类 包括当前页,每页条数,查询条件
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage, pageSize);
        Page<Setmeal> page = setmealDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增套餐
     *
     * @param setmeal       套餐信息
     * @param checkgroupIds 与套餐相关的检查组id
     */
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        //添加套餐 ,返回的类中,有自增的主键id
        setmealDao.add(setmeal);

        Integer setmealId = setmeal.getId();

        //判断整形数组数据是否有效
        if (checkgroupIds != null && checkgroupIds.length > 0) {
            setSetmealAndCheckGroup(setmealId, checkgroupIds);
        }
/*        //将图片名称保存到Redis
        savePic2Redis(setmeal.getImg());*/

        //新增套餐后需要重新生成静态页面
        generateMobileStaticHtml();

    }

    /**
     * 查询所有图片名称
     *
     * @return List<String> 图片名称集合
     */
    @Override
    public List<String> findAllImg() {
        return setmealDao.findAllImg();
    }

    /**
     * 根据id查询套餐
     *
     * @param setmealId 套餐id
     * @return 套餐
     */
    @Override
    public Setmeal findById(Integer setmealId) {

        return setmealDao.findById(setmealId);
    }

    /**
     * 根据菜单id查询检查组id
     *
     * @param setmealId 套餐id
     * @return List<Integer> 检查组id
     */
    @Override
    public List<Integer> findCheckGroupIdBysetMealId(Integer setmealId) {

        return setmealDao.findCheckGroupIdBysetMealId(setmealId);
    }

    /**
     * 修改套餐信息
     *
     * @param setmeal       套餐信息
     * @param checkgroupIds 检查组id
     */
    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        Integer setmealId = setmeal.getId();

        //删除套餐和检查组中间表
        setmealDao.deleteAssociation(setmealId);

        //添加套餐和检查组中间表
        setSetmealAndCheckGroup(setmealId, checkgroupIds);

        //修改套餐
        setmealDao.edit(setmeal);

    }

    /**
     * 根据id删除套餐
     *
     * @param setmealId 套餐id
     */
    @Override
    public void delete(Integer setmealId) {
        //删除套餐-检查组中间表
        setmealDao.deleteAssociation(setmealId);

        //删除套餐
        setmealDao.delete(setmealId);
    }

    /**
     * 查询所有套餐
     *
     * @return true or false
     */
    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    /**
     * 根据id查询套餐
     *
     * @param id 套餐id
     * @return 套餐基本信息
     */
    @Override
    public Setmeal findSetmealBasicById(Integer id) {
        return setmealDao.findSetmealBasicById(id);
    }

    /**
     * 查询套餐统计
     *
     * @return 套餐名称 预约次数
     */
    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return setmealDao.findSetmealCount();
    }

    /**
     * 添加套餐和检查组中间表
     *
     * @param setmealId     套餐id
     * @param checkgroupIds 检查组
     */
    public void setSetmealAndCheckGroup(Integer setmealId, Integer[] checkgroupIds) {

        for (Integer checkgroupId : checkgroupIds) {
            Map<String, Integer> map = new HashMap<>();
            map.put("setmealId", setmealId);
            map.put("checkgroupId", checkgroupId);
            setmealDao.setSetmealAndCheckGroup(map);
        }
    }

    /**
     * 生成静态页面
     */
    public void generateMobileStaticHtml() {
        //准备模板文件中所需的数据
        List<Setmeal> setmealList = this.findAll();

        //生成套餐列表静态页面
        generateMobileSetmealListHtml(setmealList);

        //生成套餐详情静态页面(多个)
        generateMobileSetmealDetailHtml(setmealList);

    }

    /**
     * 生成套餐列表静态页面
     *
     * @param setmealList 套餐列表
     */
    public void generateMobileSetmealListHtml(List<Setmeal> setmealList) {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("setmealList", setmealList);
        this.generateHtml("mobile_setmeal.ftl", "m_setmeal.html", dataMap);
    }


    /**
     * 生成套餐详情静态页面(多个)
     *
     * @param setmealList 套餐列表
     */
    public void generateMobileSetmealDetailHtml(List<Setmeal> setmealList) {

        for (Setmeal setmeal : setmealList) {
            Map<String, Object> map = new HashMap<>();
            map.put("setmeal", this.findById(setmeal.getId()));
            this.generateHtml("mobile_setmeal_detail.ftl", "setmeal_detail_" + setmeal.getId() + ".html", map);
        }
    }


    /**
     * 生成静态页面
     *
     * @param templateName 模板名称
     * @param htmlPageName html模板
     * @param dataMap      数据
     */
    public void generateHtml(String templateName, String htmlPageName, Map<String, Object> dataMap) {
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        Writer out ;

        try {
            //加载模板文件
            Template template = configuration.getTemplate(templateName);
            //生成数据
            File docFile = new File(outPutPath + "\\" + htmlPageName);

            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));

            //输出文件
            template.process(dataMap, out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
